package com.huidian.gitbatchprocessing.ui.service.impl;

import com.huidian.gitbatchprocessing.git.util.GitUtil;
import com.huidian.gitbatchprocessing.ui.constant.DialogConstant;
import com.huidian.gitbatchprocessing.ui.enums.BranchOperateEnum;
import com.huidian.gitbatchprocessing.ui.service.AbstractBranchOperate;
import com.huidian.gitbatchprocessing.ui.service.dto.BaseBranchDto;
import com.huidian.gitbatchprocessing.ui.service.dto.CreateBranchDto;
import com.intellij.openapi.util.text.StringUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;

/**
 * 创建分支操作
 *
 * @author zxb
 * @date 2021/6/18
 */
public class CreateBranchOperateImpl extends AbstractBranchOperate {

    @Override
    public Byte getKey() {
        return BranchOperateEnum.CREATE_BRANCH.getKey();
    }

    @Override
    public String execute(BaseBranchDto baseBranchDto, Repository repository) throws Exception {
        CreateBranchDto createBranchDto = (CreateBranchDto) baseBranchDto;
        String createBranchSourceName = createBranchDto.getSourceBranchName();
        String createBranchTargetName = createBranchDto.getTargetBranchName();
        if (StringUtil.isEmpty(createBranchSourceName)) {
            throw new RuntimeException("源分支名不能为空！");
        }
        if (StringUtil.isEmpty(createBranchTargetName)) {
            throw new RuntimeException("目标分支名不能为空！");
        }
        Git git = new Git(repository);
        GitUtil.createBranch(git, createBranchSourceName, createBranchTargetName);
        return DialogConstant.SUCCESS;
    }
}
